'''
Desc: 
Author: 陈爱国
Date: 2024-08-23 09:48:33
'''
import tkinter as tk

# 行数
row = 20
# 列数
col = 12
# 格子长度
cell_size = 30
# 高度
height = row * cell_size
# 宽度
width = col * cell_size

# 俄罗斯方块形状
shapes = {
    "O" : [(-1, -1),(0, -1), (-1, 0), (0, 0)]
}

# 不同方块的颜色
shape_colors = {
    "O" : "blue"
}


def draw_cell(canvas, row, col, color="#cccccc"):
    """
    绘制格子
    :param canvas: 画布
    :param row: 行
    :param col: 列
    :param color: 颜色
    :return:
    """
    # 格子的左上角
    x0 = col*cell_size
    y0 = row*cell_size

    # 格子右下角
    x1 = col*cell_size + cell_size
    y1 = row*cell_size + cell_size

    canvas.create_rectangle(x0, y0, x1, y1, fill=color, outline="white", width=2)


def draw_blank_board(canvas):
    """
    在画布上绘制空白背景
    :param canvas: 画布
    :return:
    """
    for ri in range(row):
        for ci in range(col):
            draw_cell(canvas, ri, ci)


def draw_shape(canvas, c, r, loc_list, color="#cccccc"):
    """
    绘制不同的俄罗斯方块
    :param canvs: 画布
    :param c: 该形状设定的原点所在列
    :param r: 该形状设定的原点所在行
    :param loc_list: 该形状的不同格子相对于自身原点的位置坐标
    :param color:
    :return:
    """
    for cell in loc_list:
        cell_c, cell_r = cell
        ri = cell_r + r
        ci = cell_c + c

        if 0 <= ri < row and 0 <= ci < col:
            draw_cell(canvas, ci, ri, color)


win = tk.Tk()

# 创建画布
canvs = tk.Canvas(win, width=width, height=height)
# 放置画布
canvs.pack()

# 绘制白色背景
draw_blank_board(canvs)

# 绘制形状
draw_shape(canvs, 3, 4, shapes["O"], shape_colors["O"])

win.mainloop()